import { createSlice } from "@reduxjs/toolkit";

import axios from "axios";

const foodStore = createSlice({
  name: "food",
  initialState: {
    foodList: [],
  },
  reducers: {
    setFoodList(state, action) {
      state.foodList = action.payload;
    },
  },
});

const { setFoodList } = foodStore.actions;
const reducer = foodStore.reducer;

function fetchFoodList(categoryCode) {
  return async function (dispatch) {
    const res = await axios({
      url: "http://localhost:9000/food/listByCategoryCode",
      method: "post",
      data: { categoryCode },
    });
    // console.log("fetch food list, res", res);
    dispatch(setFoodList(res.data.data));
  };
}

// export { setFoodList };
export { fetchFoodList };
export default reducer;
